package org.programmers.anonymous.fightrobots.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.programmers.anonymous.fightrobots.controller.DeffenseController;
import org.programmers.anonymous.fightrobots.controller.RobotController;
import org.programmers.anonymous.fightrobots.controller.WeaponController;
import org.programmers.anonymous.fightrobots.model.IDeffense;
import org.programmers.anonymous.fightrobots.model.IRobot;
import org.programmers.anonymous.fightrobots.model.IWeapon;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/robot")
public class RobotWebController implements BeanFactoryAware {

	private RobotController robotController;
	private WeaponController weaponController;
	private DeffenseController defenseController;

	@Override
	public void setBeanFactory(BeanFactory context) throws BeansException {
		robotController = (RobotController) context.getBean("robotController");
		weaponController = (WeaponController) context.getBean("weaponController");
		defenseController = (DeffenseController) context.getBean("deffenseController");

	}

	//Metodo de Bienvenida
	@RequestMapping(method = RequestMethod.GET)
	public String printWelcome(ModelMap model) 
	{
		model.addAttribute("message", "Robot Configuration");
		return "welcomeRobot";
	}

	//Metodo para visualizar los robots a partir de lista desplegable
	@RequestMapping(value ="/viewRobot/", method = RequestMethod.GET)
	public String Robots(ModelMap model)
	{
		Map<Integer,String> robotMap=new HashMap<Integer,String>();
		List<String> weaponNameList = new ArrayList<String>();
		List<String> defenseNameList = new ArrayList<String>();


		for (IRobot iRobot: robotController.getAllRobots()){
			robotMap.put(iRobot.getIdRobot(),iRobot.getName());	     		
		}
		
		for(IWeapon iWeapon: weaponController.getAllWeapons()){
			weaponNameList.add(iWeapon.getName());
		}
		
		for(IDeffense iDefense: defenseController.getAllDeffenses()){
			defenseNameList.add(iDefense.getName());
		}
		model.addAttribute("weaponRobot", weaponNameList);
		model.addAttribute("defenseRobot", defenseNameList);
		model.addAttribute("robotMap", robotMap);		
		return "viewRobots";
	}

}
